//定义js命名空间
Ext.namespace('Infrastructure.CifCust');

//CifCust FieldArray
Infrastructure.CifCust.BaseFieldsArray_N = new Ext.util.MixedCollection({
	items: [{ name: 'Id', type: 'string' },
	{ name: 'CustNo', mapping: 'Id' },
	{ name: 'CustNa', type: 'string' },
	{ name: 'CustPy', type: 'string' },
	{ name: 'CustTp', type: 'string' },
	{ name: 'CertTp', type: 'string' },
	{ name: 'CertNo', type: 'string' },
	{ name: 'ContUs', type: 'string' },
	{ name: 'OffiTl', type: 'string' },
	{ name: 'HomeTl', type: 'string' },
	{ name: 'MobiTl', type: 'string' },
	{ name: 'MailCd', type: 'string' },
	{ name: 'MailAd', type: 'string' },
	{ name: 'EMail', type: 'string' },
	{ name: 'Description', type: 'string' },
	{ name: 'IsDelete', type: 'bool' },
	{ name: 'Version', type: 'int' },
	{ name: 'CreateTime', type: 'DateTime'
	}]
});

//CifCust FieldArray
Infrastructure.CifCust.BaseFieldsArray = [
	{ name: 'Id', type: 'string' },
	{ name: 'CustNo', mapping: 'Id' },
	{ name: 'CustNa', type: 'string' },
	{ name: 'CustPy', type: 'string' },
	{ name: 'CustTp', type: 'string' },
	{ name: 'CertTp', type: 'string' },
	{ name: 'CertNo', type: 'string' },
	{ name: 'ContUs', type: 'string' },
	{ name: 'OffiTl', type: 'string' },
	{ name: 'HomeTl', type: 'string' },
	{ name: 'MobiTl', type: 'string' },
	{ name: 'MailCd', type: 'string' },
	{ name: 'MailAd', type: 'string' },
	{ name: 'EMail', type: 'string' },
	{ name: 'Description', type: 'string' },
	{ name: 'IsDelete', type: 'bool' },
	{ name: 'Version', type: 'int' },
	{ name: 'CreateTime', type: 'DateTime' }
];

//CifCust JsonReader
Infrastructure.CifCust.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.CifCust.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: Infrastructure.CifCust.BaseFieldsArray
		}, cfg));
	}
});

Infrastructure.CifCust.BaseStore = Ext.extend(Ext.data.JsonStore, {
	selUrl: null,
	updUrl: null,
	delUrl: null,
	listeners: {
		'update': function (thiz, record, operation) {
			var model = record.data;
			if (operation == Ext.data.Record.EDIT && !record.getChanges().Id) {
				//加载进度条
				Ext.MessageBox.show({
					msg: '正在提交数据到中心服务器...',
					progressText: '保存数据...',
					width: 300,
					wait: true,
					waitConfig: { interval: 1000 }
				});
				Ext.Ajax.request({
					url: this.updUrl,
					params: model,
					success: function (response, opts) {
						Ext.MessageBox.hide();
						var result = Ext.util.JSON.decode(response.responseText);
						if (result.success) {
							if (!record.get('Id'))
								record.set('Id', result.id);
							thiz.commitChanges();
						} else {
							JsHelper.ShowError(result.msg);
							thiz.rejectChanges();
						}
						thiz.reload();
					},
					failure: function (form, action) {
						Ext.MessageBox.hide();
						JsHelper.ShowActionError(action);
					}
				});
			}
		},
		'remove': function (thiz, record, index) {
			refStore = this;
			if (record.data.Id != null && record.data.Id != '') {
				concateStr = this.delUrl.indexOf('?') == -1 ? '?' : '&';
				Ext.Ajax.request({
					url: this.delUrl + concateStr + 'Id=' + record.data.Id,
					success: function (response, opts) {
						var result = Ext.util.JSON.decode(response.responseText);
						if (result.success == false) {
							JsHelper.ShowError(result.msg);
							refStore.insert(0, record);
						}
					},
					failure: function (form, action) {
						JsHelper.ShowActionError(action);
					}
				});
			}
		}
	},
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.CifCust.BaseStore.superclass.constructor.call(this, Ext.applyIf(cfg, {
			baseParams: {
				start: 0,
				limit: ALL_PAGESIZE_SETTING
			},
			url: cfg.selUrl,
			root: 'data',
			fields: this.fields,
			totalProperty: 'total'
		}));
	}
});

Infrastructure.CifCust.NewStore = Ext.extend(Infrastructure.CifCust.BaseStore, {
	//url: '/CifCust/GetPlist',
	updUrl: null,
	delUrl: null,
	selUrl: null,
	fields: Infrastructure.CifCust.BaseFieldsArray
});

//CifCust JsonStore
Infrastructure.CifCust.JsonStore = Ext.extend(Infrastructure.CifCust.BaseStore, {
	url: '/CifCust/GetPlist', //查询Url
	updUrl: '/CifCust/SaveOrUpdate', //更新记录Url
	delUrl: '/CifCust/Delete', //删除记录Url
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.CifCust.JsonStore.superclass.constructor.call(this, Ext.applyIf({
			fields: Infrastructure.CifCust.BaseFieldsArray
		}, cfg));
	}
});

//CifCust EditForm
Infrastructure.CifCust.EditForm = Ext.extend(Infrastructure.BaseForm, {
	submitUrl: '/CifCust/SaveOrUpdate',
	submitMsg: '确定要保存吗?',

	initComponent: function () {
		var itemEditForm = this;

		//Form第一列
		// 定义表单列
		var firstColumn = [{
			fieldLabel: "客户编号",
			maxLength: 20,
			readOnly: true,
			hidden: this.dataRecord == null,
			name: 'CustNo'
		}, {
			fieldLabel: "客户名称",
			maxLength: 20,
			allowBlank: false,
			name: 'CustNa'
		}, {
			fieldLabel: "名称拼音",
			maxLength: 50,
			hidden: true,
			name: 'CustPy'
		}, {
			fieldLabel: "客户类型",
			maxLength: 2,
			name: 'CustTp'
		}, {
			fieldLabel: "证件类型",
			name: 'CertTp'
		}, {
			fieldLabel: "证件号码",
			maxLength: 50,
			name: 'CertNo'
		}, {
			fieldLabel: "邮政编码",
			maxLength: 6,
			name: 'MailCd'
		}];

		//Form第二列
		var secondColumn = [{
			fieldLabel: "联系人",
			maxLength: 50,
			name: 'ContUs'
		}, {
			fieldLabel: "办公电话",
			maxLength: 20,
			name: 'OffiTl'
		}, {
			fieldLabel: "家庭电话",
			maxLength: 20,
			name: 'HomeTl'
		}, {
			fieldLabel: "手机号码",
			maxLength: 20,
			name: 'MobiTl'
		}, {
			fieldLabel: "Email",
			maxLength: 50,
			name: 'EMail'
		}];

		//Form单列
		var singleColumn = [{
			xtype: 'textfield',
			name: 'MailAd',
			fieldLabel: "地址",
			maxLength: 100,
			width: 430
		}, {
			xtype: 'textarea',
			fieldLabel: '描述',
			name: 'Description',
			height: 50,
			width: 430
		}];

		Ext.applyIf(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						allowBlank: true
					},
					items: firstColumn
				}, {
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150
					},
					items: secondColumn
				}]
			}, singleColumn],
			reader: new Infrastructure.CifCust.JsonReader()
		});

		Infrastructure.CifCust.EditForm.superclass.initComponent.call(this);

		//定义保存后执行的事件
		this.addEvents('afterSave');
	}
});

//CifCust DetailWin
Infrastructure.CifCust.EditWin = Ext.extend(Infrastructure.BaseWindow, {
	title: '客户基本信息',
	iconCls: 'user_suit',
	dataForm: new Infrastructure.CifCust.EditForm({
		submitUrl: this.submitUrl,
		dataId: this.dataId,
		dataRecord: this.selRecord,
		listeners: {
			'afterSave': function (ele, rt) {
				if (rt) {
					if (!Ext.isDefined(selRecord))
						ele.form.reset();
					itemGrid.store.reload();
					JsHelper.ShowInfo('信息更新成功!');
				}
			}
		}
	}),

	initComponent: function () {
		Infrastructure.CifCust.EditWin.superclass.initComponent.call(this);
	}
});

//CifCust GridPanel
Infrastructure.CifCust.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// User property
	menuPId: '8001',
	//基本处理Url
	baseUrl: '/CifCust/',
	//提交保存Url
	formSubmitUrl: '/CifCust/SaveOrUpdate',
	//提交保存提示信息
	formSubmitMsg: '确定要保存吗?',
	//状态
	itemState: '',

	//编辑
	itemEditWin: new Infrastructure.CifCust.EditWin({
		closeAction: 'hide',
		dataForm: new Infrastructure.CifCust.EditForm(),
		menuId: '8001'
	}),

	//initComponent
	initComponent: function () {
		//prepare

		var itemGrid = this;
		var cmdAddEntity = this.onAddEntity;
		//set store params
		var gridStore = new Infrastructure.CifCust.NewStore({
			updUrl: '/CifCust/SaveOrUpdate', //更新记录Url
			//fields: Infrastructure.CifCust.BaseFieldsArray,
			baseParams: {
				start: 0,
				limit: 3
			},
			url: '/CifCust/GetPlist',
			delUrl: '/CifCust/Delete' //删除记录Url
		});

		var gridStore_n = new Infrastructure.CifCust.JsonStore({
			root: 'data',
			totalProperty: 'total',
			url: '/CifCust/GetPlist' //查询Url
		});

		gridStore.setBaseParam('state', this.itemState);

		// The filter.
		// { dataIndex: 'Department.Name', type: 'list', options: JsHelper.GetFilterListData(Infrastructure.Params.DeptStore.data.items, 'Name', 'Name') },
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [{
				dataIndex: 'Id',
				type: 'string'
			}, {
				dataIndex: 'CustNa',
				type: 'string'
			}, {
				dataIndex: 'CustPy',
				type: 'string'
			}, {
				dataIndex: 'CustTp',
				type: 'string'
			}, {
				dataIndex: 'CertTp',
				type: 'string'
			}, {
				dataIndex: 'CertNo',
				type: 'string'
			}, {
				dataIndex: 'ContUs',
				type: 'string'
			}, {
				dataIndex: 'OffiTl',
				type: 'string'
			}, {
				dataIndex: 'HomeTl',
				type: 'string'
			}, {
				dataIndex: 'MobiTl',
				type: 'string'
			}, {
				dataIndex: 'MailCd',
				type: 'string'
			}, {
				dataIndex: 'MailAd',
				type: 'string'
			}, {
				dataIndex: 'EMail',
				type: 'string'
			}, {
				dataIndex: 'Description',
				type: 'string'
			}, {
				dataIndex: 'IsDelete',
				type: 'string'
			}, {
				dataIndex: 'Version',
				type: 'string'
			}, {
				dataIndex: 'CreateTime',
				type: 'string'
			}]
		});

		var selModel = new Ext.grid.CheckboxSelectionModel({
			//header: '',
			singleSelect: false,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					var bottomToolbar = sm.grid.getBottomToolbar();
					bottomToolbar.btnDel.setDisabled(sm.getCount() < 1);
					bottomToolbar.btnSel.setDisabled(sm.getCount() < 1);
				}
			}
		});

		var gridColumn = new Ext.grid.ColumnModel({
			columns: [new Ext.grid.RowNumberer(),
			selModel,
			{
				header: '客户编号',
				dataIndex: 'Id'
			}, {
				header: '客户名称',
				dataIndex: 'CustNa'
			}, {
				header: '名称拼音',
				dataIndex: 'CustPy'
			}, {
				header: '客户类型',
				dataIndex: 'CustTp'
			}, {
				header: '证件类型',
				dataIndex: 'CertTp'
			}, {
				header: '证件号码',
				dataIndex: 'CertNo'
			}, {
				header: '联系人',
				dataIndex: 'ContUs'
			}, {
				header: '办公电话',
				dataIndex: 'OffiTl'
			}, {
				header: '家庭电话',
				dataIndex: 'HomeTl'
			}, {
				header: '手机号码',
				dataIndex: 'MobiTl'
			}, {
				header: '邮政编码',
				dataIndex: 'MailCd'
			}, {
				header: '地址',
				dataIndex: 'MailAd'
			}, {
				header: 'Email',
				dataIndex: 'EMail'
			}],
			defaults: {
				sortable: true,
				editor: {
					xtype: 'textfield'
				}
			}
		});

		Ext.applyIf(this, {
			plugins: [gridFilter],
			store: gridStore,
			sm: selModel,
			cm: gridColumn
		});

		Infrastructure.CifCust.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/
	addEntity: function () {
		this.onShowItemWin();
	},

	showEntity: function (grid, rowIndex) {
		this.onShowItemWin(grid.store.getAt(rowIndex));
	},

	showEntity_Test: function (grid, rowIndex) {
		var custGrid = new Infrastructure.CifCust.GridPanel({
			height: 500,
			listeners: {
				'afterChoose': function (selRecord, rt) {
					if (rt) {
						tgField.setValue(selRecord.data.Id);
						custGridWin.destroy(); //关闭窗口
						tgField.fireEvent('afterSelect', tgField, selRecord, true);
					}
				}
			}
		});
		var custGridWin = new Infrastructure.BaseWindow({
			title: '请选择客户',
			iconCls: 'car',
			closeAction: 'hide',
			width: 800,
			items: [custGrid]
		});
		custGridWin.show();
		custGrid.store.load();
	},

	onShowItemWin: function (selRecord) {
		var itemGrid = this;
		var dataId = Ext.isDefined(selRecord) ? selRecord.data.Id : null;
		/*
		var itemEditForm = new Infrastructure.CifCust.EditForm({
		submitUrl: this.formSubmitUrl,
		dataId: dataId,
		dataRecord: selRecord,
		listeners: {
		'afterSave': function (ele, rt) {
		if (rt) {
		if (!Ext.isDefined(selRecord))
		ele.form.reset();
		itemGrid.store.reload();
		JsHelper.ShowInfo('信息更新成功!');
		}
		}
		}
		});

		var itemEditWin = new Infrastructure.CifCust.EditWin({
		dataForm: itemEditForm,
		menuId: this.menuPId
		});
		*/
		this.itemEditWin.show();
		if (Ext.isDefined(selRecord)) {
			this.itemEditWin.dataForm.form.loadRecord(selRecord);
		}
	}
});


Infrastructure.CifCust.CustSelectTrigger = Ext.extend(Ext.form.TriggerField, {
	fieldLabel: '联系人',
	triggerClass: 'x-form-search-trigger', //按钮样式
	readOnly: false, //只读
	initComponent: function () {
		var tgField = this;
		var chooseCust = function () {
			var custGrid = new Infrastructure.CifCust.GridPanel({
				height: 500,
				listeners: {
					'afterChoose': function (selRecord, rt) {
						if (rt) {
							tgField.setValue(selRecord.data.Id);
							custGridWin.destroy(); //关闭窗口
							tgField.fireEvent('afterSelect', tgField, selRecord, true);
						}
					}
				}
			});
			var custGridWin = new Infrastructure.BaseWindow({
				title: '请选择客户',
				iconCls: 'car',
				closeAction: 'hide',
				width: 800,
				items: [custGrid]
			});
			custGridWin.show();
			custGrid.store.load({
				params: {
					CustNa: '流氓'
				}
			});
		};

		Ext.apply(this, {
			//单击事件 
			onTriggerClick: chooseCust
		});
		Infrastructure.CifCust.CustSelectTrigger.superclass.initComponent.call(this);
		this.addEvents('afterSelect');
	}
});
